<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="档案类型名称" prop="typeName">
        <el-input
          v-model="queryParams.typeName"
          placeholder="请输入档案类型名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="档案类型编码" prop="typeCode">
        <el-input
          v-model="queryParams.typeCode"
          placeholder="请输入档案类型编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="组织" prop="pkOrg">
        <el-input
          v-model="queryParams.pkOrg"
          placeholder="请输入组织"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="编码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="助记码" prop="mnecode">
        <el-input
          v-model="queryParams.mnecode"
          placeholder="请输入助记码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上级档案" prop="pid">
        <el-input
          v-model="queryParams.pid"
          placeholder="请输入上级档案"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上级档案编码" prop="pcode">
        <el-input
          v-model="queryParams.pcode"
          placeholder="请输入上级档案编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="启用状态" prop="enablestate">
        <el-input
          v-model="queryParams.enablestate"
          placeholder="请输入启用状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="自定义项1" prop="def1">
        <el-input
          v-model="queryParams.def1"
          placeholder="请输入自定义项1"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="自定义项2" prop="def2">
        <el-input
          v-model="queryParams.def2"
          placeholder="请输入自定义项2"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="自定义项3" prop="def3">
        <el-input
          v-model="queryParams.def3"
          placeholder="请输入自定义项3"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="自定义项4" prop="def4">
        <el-input
          v-model="queryParams.def4"
          placeholder="请输入自定义项4"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="自定义项5" prop="def5">
        <el-input
          v-model="queryParams.def5"
          placeholder="请输入自定义项5"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="creator">
        <el-input
          v-model="queryParams.creator"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="creationtime">
        <el-date-picker clearable
          v-model="queryParams.creationtime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="修改人" prop="modifier">
        <el-input
          v-model="queryParams.modifier"
          placeholder="请输入修改人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="修改时间" prop="modifiedtime">
        <el-date-picker clearable
          v-model="queryParams.modifiedtime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择修改时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="备注" prop="memo">
        <el-input
          v-model="queryParams.memo"
          placeholder="请输入备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
	    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:archive:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="archiveList"
      row-key="pkDefdoc"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      :load="getChildrens"
      lazy
      style="width: 100%"
      height="calc(100vh - 85px)"
      :row-class-name="tableRowClassName"
    >
      <el-table-column label="档案类型名称" prop="typeName" />
      <el-table-column label="档案类型编码" align="center" prop="typeCode" />
      <el-table-column label="组织" align="center" prop="pkOrg" />
      <el-table-column label="编码" align="center" prop="code" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="助记码" align="center" prop="mnecode" />
      <el-table-column label="上级档案" align="center" prop="pid" />
      <el-table-column label="上级档案编码" align="center" prop="pcode" />
      <el-table-column label="启用状态" align="center" prop="enablestate" />
      <el-table-column label="自定义项1" align="center" prop="def1" />
      <el-table-column label="自定义项2" align="center" prop="def2" />
      <el-table-column label="自定义项3" align="center" prop="def3" />
      <el-table-column label="自定义项4" align="center" prop="def4" />
      <el-table-column label="自定义项5" align="center" prop="def5" />
      <el-table-column label="创建人" align="center" prop="creator" />
      <el-table-column label="创建时间" align="center" prop="creationtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.creationtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改人" align="center" prop="modifier" />
      <el-table-column label="修改时间" align="center" prop="modifiedtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.modifiedtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="memo" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:archive:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['system:archive:add']"
          >新增</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:archive:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改自定义档案对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="档案类型名称" prop="typeName">
          <el-input v-model="form.typeName" placeholder="请输入档案类型名称" />
        </el-form-item>
        <el-form-item label="档案类型编码" prop="typeCode">
          <el-input v-model="form.typeCode" placeholder="请输入档案类型编码" />
        </el-form-item>
        <el-form-item label="组织" prop="pkOrg">
          <el-input v-model="form.pkOrg" placeholder="请输入组织" />
        </el-form-item>
        <el-form-item label="编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入编码" />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="助记码" prop="mnecode">
          <el-input v-model="form.mnecode" placeholder="请输入助记码" />
        </el-form-item>
        <el-form-item label="上级档案" prop="pid">
          <el-input v-model="form.pid" placeholder="请输入上级档案" />
        </el-form-item>
        <el-form-item label="上级档案编码" prop="pcode">
          <treeselect v-model="form.pcode" :options="archiveOptions" :normalizer="normalizer" placeholder="请选择上级档案编码" />
        </el-form-item>
        <el-form-item label="启用状态" prop="enablestate">
          <el-input v-model="form.enablestate" placeholder="请输入启用状态" />
        </el-form-item>
        <el-form-item label="自定义项1" prop="def1">
          <el-input v-model="form.def1" placeholder="请输入自定义项1" />
        </el-form-item>
        <el-form-item label="自定义项2" prop="def2">
          <el-input v-model="form.def2" placeholder="请输入自定义项2" />
        </el-form-item>
        <el-form-item label="自定义项3" prop="def3">
          <el-input v-model="form.def3" placeholder="请输入自定义项3" />
        </el-form-item>
        <el-form-item label="自定义项4" prop="def4">
          <el-input v-model="form.def4" placeholder="请输入自定义项4" />
        </el-form-item>
        <el-form-item label="自定义项5" prop="def5">
          <el-input v-model="form.def5" placeholder="请输入自定义项5" />
        </el-form-item>
        <el-form-item label="创建人" prop="creator">
          <el-input v-model="form.creator" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="创建时间" prop="creationtime">
          <el-date-picker clearable
            v-model="form.creationtime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="修改人" prop="modifier">
          <el-input v-model="form.modifier" placeholder="请输入修改人" />
        </el-form-item>
        <el-form-item label="修改时间" prop="modifiedtime">
          <el-date-picker clearable
            v-model="form.modifiedtime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择修改时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="memo">
          <el-input v-model="form.memo" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listArchive, getArchive, delArchive, addArchive, updateArchive, getroot, getchildren } from "@/api/factory/archive/archive";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Archive",
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 自定义档案表格数据
      archiveList: [],
      // 自定义档案树选项
      archiveOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        typeName: null,
        typeCode: null,
        pkOrg: null,
        code: null,
        name: null,
        mnecode: null,
        pid: null,
        pcode: null,
        enablestate: null,
        def1: null,
        def2: null,
        def3: null,
        def4: null,
        def5: null,
        creator: null,
        creationtime: null,
        modifier: null,
        modifiedtime: null,
        memo: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询自定义档案列表 */
    // getList() {
    //   this.loading = true;
    //   listArchive(this.queryParams).then(response => {
    //     this.archiveList = this.handleTree(response.data, "code", "pcode");
    //     this.loading = false;
    //   });
    // },


     getList() {
      this.loading = true;
      this.queryParams.pcode = 'root'
      getroot(this.queryParams).then(response => {
        this.archiveList = response.rows.map((item, index) =>{
                 item.hasChildren = true;
                 return item
             })
             this.total = response.total;
             this.loading = false;
      });
      
    },

  getChildrens(tree,treeNode, resolve){
    this.queryParams.pkDefdoc = tree.pkDefdoc
    getchildren(this.queryParams).then((response) => {
      
        var data
        if(response.data.length&&response.data.length > 0){
            let data = response.data.map((item, index) =>{
              if(item.type === 'root'){
                item.hasChildren = false;
              }else{
                item.hasChildren = true;
              }  
              return item
            })
            resolve(data)
        }else{
            tree.hasChildren = false
        }
    });
},
    /** 转换自定义档案数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.code,
        label: node.name,
        children: node.children
      };
    },
	/** 查询自定义档案下拉树结构 */
    getTreeselect() {
      listArchive().then(response => {
        this.archiveOptions = [];
        const data = { code: root, name: 'root', children: [] };
        data.children = this.getChildrens(response.data);
        this.archiveOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        pkDefdoc: null,
        typeName: null,
        typeCode: null,
        pkOrg: null,
        code: null,
        name: null,
        mnecode: null,
        pid: null,
        pcode: null,
        enablestate: null,
        def1: null,
        def2: null,
        def3: null,
        def4: null,
        def5: null,
        creator: null,
        creationtime: null,
        modifier: null,
        modifiedtime: null,
        memo: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.code) {
        this.form.pcode = row.code;
        this.form.typeName = row.typeName;
        this.form.typeCode = row.typeCode;
      } else {
        this.form.pcode = root;
      }
      this.open = true;
      this.title = "添加自定义档案";
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.pcode = row.code;
      }
      getArchive(row.pkDefdoc).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改自定义档案";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.pkDefdoc != null) {
            updateArchive(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addArchive(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除自定义档案编号为"' + row.pkDefdoc + '"的数据项？').then(function() {
        return delArchive(row.pkDefdoc);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
